package com.glory.brepda.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.brepda.R;
import com.glory.brepda.base.MyApp;
import com.glory.brepda.entity.EdcSetBinEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;

import java.util.List;

import androidx.annotation.Nullable;

public class EdcSetBinAdapter extends BaseQuickAdapter<EdcSetBinEntity, BaseViewHolder> {
    private int mMainqty = 0;

    public EdcSetBinAdapter(@Nullable List<EdcSetBinEntity> data) {
        super(R.layout.adapter_edcset_bin, data);
    }

    public void setMainqty(int mMainqty) {
        this.mMainqty = mMainqty;
    }

    @Override
    protected void convert(BaseViewHolder helper, EdcSetBinEntity item) {
        helper.setText(R.id.tvName, item.getNAME())
                .setText(R.id.tvDesc, item.getDESCRIPTION())
                .setText(R.id.etValue, String.valueOf(item.getVALUE()))
                .setText(R.id.tvACTIONPARAM1, String.valueOf(item.getACTIONPARAM1()))
                .setText(R.id.tvACTIONPARAM3, String.valueOf(item.getACTIONPARAM3()))
                .setText(R.id.tvACTIONPARAM4, String.valueOf(item.getACTIONPARAM4()));

        EditText etValue = helper.getView(R.id.etValue);
        EditText etComment = helper.getView(R.id.etComment);

        setEditListener(etValue, item, "Y");
        setEditListener(etComment, item);
    }

    private void setEditListener(EditText editText, EdcSetBinEntity item, String type) {
        if (editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }

        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0) {
                    if (type.equals("Y")) {
                        try {
                            double value = Double.parseDouble(s.toString());
                            compare(value, item, editText);
                        } catch (NumberFormatException e) {
                            e.printStackTrace();
                        }

                    }


                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                String comment = s.toString().equals("") ? "0" : s.toString();
                if (type.equals("Y")) {
                    item.setVALUE(Integer.parseInt(comment));
                } else {
                    item.setCOMMENT(comment);
                }
            }
        };

        editText.addTextChangedListener(watcher);
        editText.setTag(watcher);
    }

    private void setEditListener(EditText editText, EdcSetBinEntity item) {
        if (editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }

        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                item.setCOMMENT(s.toString());
            }
        };

        editText.addTextChangedListener(watcher);
        editText.setTag(watcher);
    }


    private void compare(double value, EdcSetBinEntity item, EditText editText) {
        String maxValue = item.getUSL();
        String minValue = item.getLSL();
        if (item.getSPECTYPE().equals("NUMBER")) {
            if (!FreeApi_StringUtils.isEmpty(maxValue) && !FreeApi_StringUtils.isEmpty(minValue)) {
                double maxValues = Double.parseDouble(maxValue) / 100 * mMainqty;
                double minValues = Double.parseDouble(minValue) / 100 * mMainqty;
                if (minValues <= value && value <= maxValues) {
                    editText.setTextColor(MyApp.app.getResources().getColor(R.color.black));
                } else {
                    editText.setTextColor(MyApp.app.getResources().getColor(R.color.red));
                }
            } else if (FreeApi_StringUtils.isEmpty(maxValue) && !FreeApi_StringUtils.isEmpty(minValue)) {
                double minValue1 = Double.parseDouble(minValue) / 100 * mMainqty;
                if (value >= minValue1) {
                    editText.setTextColor(MyApp.app.getResources().getColor(R.color.black));
                } else {
                    editText.setTextColor(MyApp.app.getResources().getColor(R.color.red));
                }
            } else if (!FreeApi_StringUtils.isEmpty(maxValue) && FreeApi_StringUtils.isEmpty(minValue)) {
                double maxValue1 = Double.parseDouble(maxValue) / 100 * mMainqty;
                if (value <= maxValue1) {
                    editText.setTextColor(MyApp.app.getResources().getColor(R.color.black));
                } else {
                    editText.setTextColor(MyApp.app.getResources().getColor(R.color.red));
                }
            } else {
                editText.setTextColor(MyApp.app.getResources().getColor(R.color.black));
            }
        } else if (item.getSPECTYPE().equals("PERCENT")) {
            if (!FreeApi_StringUtils.isEmpty(maxValue) && !FreeApi_StringUtils.isEmpty(minValue)) {
                double maxValues = mMainqty * Double.parseDouble(maxValue);
                double minValues = mMainqty * Double.parseDouble(minValue);
                if (minValues <= value && value <= maxValues) {
                    editText.setTextColor(MyApp.app.getResources().getColor(R.color.black));
                } else {
                    editText.setTextColor(MyApp.app.getResources().getColor(R.color.red));
                }
            } else if (FreeApi_StringUtils.isEmpty(maxValue) && !FreeApi_StringUtils.isEmpty(minValue)) {
                double minValue1 = mMainqty * Double.parseDouble(minValue);
                if (value >= minValue1) {
                    editText.setTextColor(MyApp.app.getResources().getColor(R.color.black));
                } else {
                    editText.setTextColor(MyApp.app.getResources().getColor(R.color.red));
                }
            } else if (!FreeApi_StringUtils.isEmpty(maxValue) && FreeApi_StringUtils.isEmpty(minValue)) {
                double maxValue1 = mMainqty * Double.parseDouble(maxValue);
                if (value <= maxValue1) {
                    editText.setTextColor(MyApp.app.getResources().getColor(R.color.black));
                } else {
                    editText.setTextColor(MyApp.app.getResources().getColor(R.color.red));
                }
            } else {
                editText.setTextColor(MyApp.app.getResources().getColor(R.color.black));
            }
        }

    }
}
